// 用户模块
export default {
  namespaced: true,

  state: {
    userInfo: null,
    token: localStorage.getItem('token') || ''
  },

  mutations: {
    SET_USER_INFO(state, userInfo) {
      state.userInfo = userInfo
    },

    SET_TOKEN(state, token) {
      state.token = token
      localStorage.setItem('token', token)
    },

    CLEAR_USER_INFO(state) {
      state.userInfo = null
      state.token = ''
      localStorage.removeItem('token')
    }
  },

  actions: {
    // 设置用户信息
    setUserInfo({ commit }, userInfo) {
      commit('SET_USER_INFO', userInfo)
    },

    // 设置token
    setToken({ commit }, token) {
      commit('SET_TOKEN', token)
    },

    // 登出
    logout({ commit }) {
      commit('CLEAR_USER_INFO')
    }
  },

  getters: {
    userInfo: state => state.userInfo,
    token: state => state.token,
    isLogin: state => !!state.token
  }
}